import { useHandleSocketStore } from '@/stores/handleSocket'
import { ElMessage } from 'element-plus'
import { getEqByCode, getEqControlItem } from "@/api/equipment"
import { useControlDetailStore } from "@/stores/eqControl"
import { UseWs } from '@/stores/wsToken'
import router from '@/router'
import { getMonitorUrl, getEqDetails } from '@/api/equipment'
import { useMessageStore } from '@/stores/message'
import { useAlertDetailStore } from '@/stores/alertDeail'
import { getGeneralValues } from "@/api/general"
import { useEqDetailStore } from '@/stores/eqInfo'
import { useFloorStore } from '@/stores/floor'
import { useAppStore } from '@/stores/modules/app';
import { useRouteInfoStore } from '@/stores/routeInfo'
import { getLocationByLocationId } from '@/api/system'
export const handleSocketMessage = async (data: any) => {
    const UseControlDetailStore = useControlDetailStore()
    const handleSocketStore = useHandleSocketStore()
    const messageStore = useMessageStore()
    const alertDeail = useAlertDetailStore()
    const eqdetail = useEqDetailStore()
    const routeInfo = useRouteInfoStore()
    const { setActive, setItem } = useFloorStore();



    if (!data?.type) {
        return
    }

    if (data.type === 'showDialog') {


        handleSocketStore.SetShowDialog(true)
        handleSocketStore.SetequipmentData(data.param)
    }
    if (data.type === 'findItAsset') {
        if (!data.data) {
            ElMessage.closeAll()
            ElMessage.error('找不到该资产')
        } else {
            ElMessage.success('定位成功')
        }
    }
    if (data.type === 'default') {


    }

    // 处理综合态势导航标签
    if (data.type === 'routeInfo') {
        let infoData = []
        routeInfo.setTitle(data.data)
        switch (data.data) {
            case '综合态势':
                infoData = [
                    { name: '设备类型', value: 16 },
                    { name: '设备总数', value: 845 },
                    { name: '今日告警事件总数', value: 2570 },
                    { name: '今日AI事件总数', value: 657 },
                    { name: '今日设备事件总数', value: 393 },
                ]
                routeInfo.Setinfo(infoData)
                routeInfo.SetShowDialog(true)
                routeInfo.Setpath('comprehensiveSituation')
                break
            case '安防数据':
                infoData = [
                    { name: '今日监控报警数量', value: 290 },
                    { name: '监控在线率', value: '98%' },
                    { name: '离线设备数', value: 8 },
                    { name: '异常设备数', value: 22 },
                ]
                routeInfo.Setinfo(infoData)
                routeInfo.SetShowDialog(true)
                routeInfo.Setpath('smartSecurity')
                break
            case '通行数据':
                infoData = [
                    { name: '今日人员通行', value: 22 },
                    { name: '今日车辆通行', value: 10 },
                    { name: '门禁告警总数', value: 20 },
                    { name: '车辆告警总数', value: 4 },
                ]
                routeInfo.Setinfo(infoData)
                routeInfo.SetShowDialog(true)
                routeInfo.Setpath('smartAccess')
                break
            case '能耗数据':
                infoData = [
                    { name: '今日能耗', value: '220kgce' },
                    { name: '今日耗电量', value: '320kgce' },
                    { name: '今日耗水量', value: '420kgce' },
                    { name: '今日耗气量', value: '520kgce' },
                    { name: '设备能耗异常总数', value: 20 },
                ]
                routeInfo.Setinfo(infoData)
                routeInfo.SetShowDialog(true)
                routeInfo.Setpath('energy')
                break
            case '资产数据':
                infoData = [
                    { name: 'IT资产总数', value: 325 },
                    { name: 'IT资产告警总数', value: 425 },
                ]
                routeInfo.Setinfo(infoData)
                routeInfo.SetShowDialog(true)
                routeInfo.Setpath('itAsset')
                break
            case '7楼':
                infoData = [
                    { name: '企业概述', value: '云扬科技自成立以来，坚持技术创新、产品创新和业务模式创新的理念。重点围绕大数据BI、智慧园区AIoT、智能制造领域，不断加强自主研发力度，已为数千行业客户提供专业的数字化解决方案，覆盖制造、建筑、商贸等各类型企业和政府、医疗、教育等行业，帮助客户降本增效，让技术创造核心竞争优势。' },
                ]
                routeInfo.Setinfo(infoData)
                routeInfo.SetShowDialog(true)
                routeInfo.Setpath('')
                break
            case '6楼':
                infoData = [
                    { name: '企业概述', value: '广东云扬科技有限公司作为一家粤东知名的IT综合服务商，以大数据BI、智慧园区AIoT、智能制造、企业数字化规划咨询、业务系统、硬件集成及信息技术服务为主要业务。覆盖企业数字化的各阶段，为企业客户提供全生命周期的数字化解决方案。' },
                ]
                routeInfo.Setinfo(infoData)
                routeInfo.SetShowDialog(true)
                routeInfo.Setpath('')
                break
        }
    }
    if (data.type === 'control') {
        if (data.data == '数据中心电压柜') {
            switch (data.data) {
                case '数据中心电压柜':
                    let eqBaseDetail1 = [
                        { name: 'A相电压', value: '216.9V' },
                        { name: 'C相电压', value: '216.9V' },
                        { name: 'B相电压', value: '216.9V' },
                        { name: 'A相电流', value: '22.5A' },
                        { name: 'B相电流', value: '22.5A' },
                        { name: 'C相电流', value: '22.5A' },
                    ]
                    eqdetail.setinfoItem(eqBaseDetail1)
                    eqdetail.SetEqName('数据中心电压柜')
                    eqdetail.SetShow(true)
                    break
            }
            return
        }
        const res = await getEqByCode(data.data)
        let eqName = res.data.name
        let eId = res.data.id
        let estatus = res.data.status
        let locationid = res.data.locationId
        const location = await getLocationByLocationId(locationid)


        const index = messageStore.$state.unreadMessageList.findIndex(item => item.equipmentId === eId)

        if (index > -1) {
            alertDeail.SetMessage(messageStore.$state.unreadMessageList[index])
            alertDeail.SetShow(true)

            return
        }
        if (data.data == '数据中心烟雾传感器' || data.data == '数据中心温湿度传感器' || data.data == '数据中心漏水绳' || data.data == '信锐ac控制器' || data.data == '深信服防火墙' || data.data == '数据中心机柜-SERVER3') {
            let eqBaseDetail = []
            switch (data.data) {
                case '数据中心温湿度传感器':
                    let hcode = 'DATA_CENTER_SITUATION_HUMDITITY'
                    let scode = 'DATA_CENTER_SITUATION'
                    let item = []
                    const res1 = await getGeneralValues([{ code: hcode }, { code: scode }])
                    if (JSON.stringify(res1.data) === '{}') {

                        ElMessage.warning('暂无数据')
                    }
                    else {
                        item.push({ name: '温度', value: res1.data['DATA_CENTER_SITUATION'] ? res1.data['DATA_CENTER_SITUATION'].data + ' °c' : '' })
                        item.push({ name: '湿度', value: res1.data['DATA_CENTER_SITUATION_HUMDITITY'] ? res1.data['DATA_CENTER_SITUATION_HUMDITITY'].data + ' %RH' : '' })
                        eqdetail.setinfoItem(item)
                        eqdetail.SetEqName(eqName)
                        eqdetail.SetEqId(res.data.id)
                        eqdetail.Setestatus(estatus)
                        eqdetail.Setlocation(location.data ? location.data.name : '')
                        eqdetail.SetShow(true)
                    }


                    break;
                case '数据中心漏水绳':
                    let code = 'DATA_CENTER_SITUATION_DRAIN'
                    let item1 = []
                    const res2 = await getGeneralValues([{ code: code }])


                    if (JSON.stringify(res2.data) === '{}') {

                        ElMessage.warning('暂无数据')
                    } else {
                        item1.push({ name: '状态', value: res2.data['DATA_CENTER_SITUATION_DRAIN'] ? res2.data['DATA_CENTER_SITUATION_DRAIN'].data : '' })
                        eqdetail.setinfoItem(item1)
                        eqdetail.SetEqName(eqName)
                        eqdetail.SetEqId(res.data.id)
                        eqdetail.Setestatus(estatus)
                        eqdetail.Setlocation(location.data ? location.data.name : '')
                        eqdetail.SetShow(true)
                    }

                    break;
                case '数据中心烟雾传感器':
                    eqdetail.setinfoItem([{ name: '状态', value: '无烟' }])
                    eqdetail.SetEqName(eqName)
                    eqdetail.SetEqId(res.data.id)
                    eqdetail.Setestatus('1')
                    eqdetail.Setlocation(location.data ? location.data.name : '')
                    eqdetail.SetShow(true)
                    break;
                case '信锐ac控制器':
                // const eqres = await getEqDetails(eId)
                // const eqbasedetail = eqres.data.extendProperty.basExtendPropertyList

                // eqbasedetail.forEach((item: any) => {
                //     eqBaseDetail.push({ name: item.name, value: item.defaultValue })
                // })
                // eqdetail.setinfoItem(eqBaseDetail)
                // eqdetail.SetEqName(eqName)
                // eqdetail.SetEqId(res.data.id)
                // eqdetail.SetShow(true)

                case '深信服防火墙':
                // const eqres = await getEqDetails(eId)
                // const eqbasedetail = eqres.data.extendProperty.basExtendPropertyList

                // eqbasedetail.forEach((item: any) => {
                //     eqBaseDetail.push({ name: item.name, value: item.defaultValue })
                // })
                // eqdetail.setinfoItem(eqBaseDetail)
                // eqdetail.SetEqName(eqName)
                // eqdetail.SetEqId(res.data.id)
                // eqdetail.SetShow(true)
                // break
                case '数据中心机柜-SERVER3':
                    const eqres = await getEqDetails(eId)
                    const eqbasedetail = eqres.data.extendProperty.basExtendPropertyList

                    eqbasedetail.forEach((item: any) => {
                        eqBaseDetail.push({ name: item.name, value: item.defaultValue })
                    })
                    eqdetail.setinfoItem(eqBaseDetail)
                    eqdetail.SetEqName(eqName)
                    eqdetail.SetEqId(res.data.id)
                    eqdetail.Setestatus(estatus)
                    eqdetail.Setlocation(location.data ? location.data.name : '')
                    eqdetail.SetShow(true)
                    break
                default:
                    break;
            }
            return
        }
        const controlRes = await getEqControlItem(res.data.id)
        UseControlDetailStore.SetShow(true)
        UseControlDetailStore.Setestatus(estatus)
        UseControlDetailStore.SeteqCode(data.data)
        UseControlDetailStore.SetEqName(eqName)
        UseControlDetailStore.SetControlItem(controlRes.data)
        UseControlDetailStore.Setlocation(location.data ? location.data.name : '')
        UseControlDetailStore.SetEqId(res.data.id)
    }
    if (data.type === 'EQ_PANEL') {
        const res = await getEqByCode(data.data)
        let eId = res.data.id
        let locationid = res.data.locationId
        const location = await getLocationByLocationId(locationid)
        const index = messageStore.$state.unreadMessageList.findIndex(item => item.equipmentId === eId)
        if (index > -1) {
            if (messageStore.$state.unreadMessageList[index].alertGrade == 'INFO') {
                const video = await getMonitorUrl(res.data.id)

                handleSocketStore.Setsrc((<any>video).data)
                handleSocketStore.SetShowDialog(true)
                handleSocketStore.SetequipmentData(res.data)
                return
            }
            alertDeail.SetMessage(messageStore.$state.unreadMessageList[index])
            alertDeail.SetShow(true)
            return
        }
        const video = await getMonitorUrl(res.data.id)

        handleSocketStore.Setsrc((<any>video).data)
        handleSocketStore.Setlocation(location.data ? location.data.name : '')
        handleSocketStore.SetShowDialog(true)
        handleSocketStore.SetequipmentData(res.data)

    }
    if (data.type === 'changeRouter') {
        router.push(data.data)


    }
    if (data.type === '开启面板') {
    }
    if (data.type == '进入空间') {
        switch (data.data) {
            case 'F7':
                setActive(1);
                setItem(2);
                break;
            case 'F6':
                setActive(1);
                setItem(1);
                break;
            case 'test':
                setActive(2);
                setItem(-1);
                break;
            default:
                setActive(-1);
                setItem(-1);
                break;
        }
    }
    if (data.type == '退出空间') {
        switch (data.data) {
            case 'F7':
                setActive(1);
                setItem(-1);
                break;
            case 'F6':
                setActive(1);
                setItem(-1);
                break;
            case '29BF2153-15C2-4E4B-9BFB-BDD5F2557467' || 'test':
                setActive(2);
                setItem(-1);
                break;
            default:

                break;
        }
    }
    if (data.type == '进入建筑') {
        switch (data.data) {
            case '29BF2153-15C2-4E4B-9BFB-BDD5F2557467':
                setActive(1);
                setItem(-1);
                break;
            default:

                break;
        }
    }
}

